//
//  RectangleProgressView.m
//  矩形进度条
//
//  Created by 田涛 on 2017/9/27.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "RectangleProgressView.h"

@interface RectangleProgressView () {
    CGFloat _width;
    CGFloat _height;
    CGFloat _topLeftAndRightWidth;
    
    CGFloat _allProgress;
    CGFloat _topRightProgress;
    CGFloat _rightProgress;
    CGFloat _bottomProgress;
    CGFloat _leftProgress;
    CGFloat _topLeftProgress;
}
/** 进度*/
@property (nonatomic, assign)  CGFloat progress;
/** 颜色*/
@property (nonatomic, strong)  UIColor *myColor;
/** 是否结束*/
@property (nonatomic, assign)  BOOL isFinished;
/** 赢或输*/
@property (nonatomic, assign)  RectangleProgressViewState state;
/** 立场*/
@property (nonatomic, assign)  RectangleProgressViewStandPoint standPoint;
@end

@implementation RectangleProgressView

- (instancetype)initWithFrame:(CGRect)frame progress:(CGFloat)progress standpoint:(RectangleProgressViewStandPoint)standpoint isFinished:(BOOL)finish isWinOrLose:(RectangleProgressViewState)state {
    if (self = [super initWithFrame:frame]) {
        self.progress   = progress;
        self.standPoint = standpoint;
        self.isFinished = finish;
        self.state      = state;
        
        if (self.standPoint == RectangleProgressViewStandPointPositive) {
            self.myColor = [UIColor colorWithRed:(154/255.0) green:(0/255.0) blue:(249/255.0) alpha:1];
        } else {
            self.myColor = [UIColor colorWithRed:(74/255.0) green:(125/255.0) blue:(249/255.0) alpha:1];
        }
        
        [self setup];
    }
    return self;
}

- (void)setup {
    self.backgroundColor = [UIColor clearColor];

    _width  = self.frame.size.width;
    _height = self.frame.size.height;
    _topLeftAndRightWidth = _width /2 - _width *0.4/2;  // 顶部 左边和右边的宽度
    
    _allProgress = _topLeftAndRightWidth *2 + _width + _height *2; // 总长度
    _topRightProgress = _topLeftAndRightWidth / _allProgress;                 // 顶部 右边 进度
    _rightProgress    = _height /_allProgress +_topRightProgress;             // 右边 进度
    _bottomProgress   = _width /_allProgress +_rightProgress;                 // 底部 进度
    _leftProgress     = _height /_allProgress +_bottomProgress;               // 左边 进度
    _topLeftProgress  = 1;                                                    // 顶部 左边 进度
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    /** 画矩形*/
    [self drawSquare];
    
    if (self.isFinished == NO) { // 没有结束 ，画进度条
        UIBezierPath *path = [UIBezierPath bezierPath];
        path.lineWidth = 4;
        
        path.lineCapStyle  = kCGLineCapSquare;  // 起点、重点样式
        path.lineJoinStyle = kCGLineCapSquare;  // 拐点样式
        
        if (_progress <= _topRightProgress) {
            [self drawProgressSquareTopRightWithPath:path];
        } else if (_progress <= _rightProgress) {
            [self drawProgressSquareRightWithPath:path];
        } else if (_progress <= _bottomProgress) {
            [self drawProgressSquareBottomWithPath:path];
        } else if (_progress <= _leftProgress) {
            [self drawProgressSquareLeftWithPath:path];
        } else if (_progress <= _topLeftProgress) {
            [self drawProgressSquareTopLeftWithPath:path];
        }
        
        // 设置线条颜色
        UIColor *strokeColor = self.myColor;
        [strokeColor set];
        [path stroke];
    }
}


/**
 *  画背景矩形
 */
- (void)drawSquare {
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    path.lineWidth = 4;
    
    path.lineCapStyle  = kCGLineCapSquare;  // 起点、重点样式
    path.lineJoinStyle = kCGLineCapSquare;  // 拐点样式
    
    [path moveToPoint:CGPointMake(_width /2 + _width *0.4 /2 , 0)];
    [path addLineToPoint:CGPointMake(_width, 0)];
    [path addLineToPoint:CGPointMake(_width, _height)];
    [path addLineToPoint:CGPointMake(0, _height)];
    [path addLineToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(_width /2 - _width *0.4 /2, 0)];
    
    // 设置线条颜色
    UIColor *strokeColor;
    if (_isFinished == YES) {
        switch (self.state) {
            case RectangleProgressViewStateWin:
                strokeColor = self.myColor;
                break;
            case RectangleProgressViewStateLose:
                strokeColor = self.myColor;
                break;
                
            default:
                break;
        }
    } else {
        strokeColor = [UIColor colorWithRed:(206/255.0) green:(206/255.0) blue:(206/255.0) alpha:1];
    }
    [strokeColor set];
    [path stroke];
}

#pragma mark - 五种进度状态
/** 第一种 顶部右边*/
- (void)drawProgressSquareTopRightWithPath:(UIBezierPath *)path {
    if (self.standPoint == RectangleProgressViewStandPointPositive) {
        [path moveToPoint:CGPointMake(_width /2 - _width *0.4 /2 , 0)];
        [path addLineToPoint:CGPointMake(_width /2 - _width *0.4 /2  - _allProgress *self.progress, 0)];
    } else if (self.standPoint == RectangleProgressViewStandPointReverse) {
        [path moveToPoint:CGPointMake(_width /2 + _width *0.4 /2 , 0)];
        [path addLineToPoint:CGPointMake(_width /2 + _width *0.4 /2  + _allProgress *self.progress, 0)];
    }
}

/** 第二种 右边*/
- (void)drawProgressSquareRightWithPath:(UIBezierPath *)path {
    if (self.standPoint == RectangleProgressViewStandPointPositive) {
        [path moveToPoint:CGPointMake(_width /2 - _width *0.4 /2 , 0)];
        [path addLineToPoint:CGPointMake(0, 0)];
        [path addLineToPoint:CGPointMake(0, _allProgress *(self.progress - _topRightProgress))];
    } else if (self.standPoint == RectangleProgressViewStandPointReverse) {
        [path moveToPoint:CGPointMake(_width /2 + _width *0.4 /2 , 0)];
        [path addLineToPoint:CGPointMake(_width, 0)];
        [path addLineToPoint:CGPointMake(_width, _allProgress *(self.progress - _topRightProgress))];
    }
}

/** 第三种 底部*/
- (void)drawProgressSquareBottomWithPath:(UIBezierPath *)path {
    if (self.standPoint == RectangleProgressViewStandPointPositive) {
        [path moveToPoint:CGPointMake(_width /2 - _width *0.4 /2 , 0)];
        [path addLineToPoint:CGPointMake(0, 0)];
        [path addLineToPoint:CGPointMake(0, _height)];
        [path addLineToPoint:CGPointMake(0 + _allProgress *  (self.progress - _rightProgress), _height)];
    } else if (self.standPoint == RectangleProgressViewStandPointReverse) {
        [path moveToPoint:CGPointMake(_width /2 + _width *0.4 /2 , 0)];
        [path addLineToPoint:CGPointMake(_width, 0)];
        [path addLineToPoint:CGPointMake(_width, _height)];
        [path addLineToPoint:CGPointMake(_width - _allProgress *  (self.progress - _rightProgress), _height)];
    }
}

/** 第四种 左边*/
- (void)drawProgressSquareLeftWithPath:(UIBezierPath *)path {
    if (self.standPoint == RectangleProgressViewStandPointPositive) {
        [path moveToPoint:CGPointMake(_width /2 - _width *0.4 /2 , 0)];
        [path addLineToPoint:CGPointMake(0, 0)];
        [path addLineToPoint:CGPointMake(0, _height)];
        [path addLineToPoint:CGPointMake(_width, _height)];
        [path addLineToPoint:CGPointMake(_width, _height - _allProgress * (self.progress - _bottomProgress))];
    } else if (self.standPoint == RectangleProgressViewStandPointReverse) {
        [path moveToPoint:CGPointMake(_width /2 + _width *0.4 /2 , 0)];
        [path addLineToPoint:CGPointMake(_width, 0)];
        [path addLineToPoint:CGPointMake(_width, _height)];
        [path addLineToPoint:CGPointMake(0, _height)];
        [path addLineToPoint:CGPointMake(0, _height - _allProgress * (self.progress - _bottomProgress))];
    }
}

/** 第五种 顶部左边*/
- (void)drawProgressSquareTopLeftWithPath:(UIBezierPath *)path {
    if (self.standPoint == RectangleProgressViewStandPointPositive) {
        [path moveToPoint:CGPointMake(_width /2 - _width *0.4 /2 , 0)];
        [path addLineToPoint:CGPointMake(0, 0)];
        [path addLineToPoint:CGPointMake(0, _height)];
        [path addLineToPoint:CGPointMake(_width, _height)];
        [path addLineToPoint:CGPointMake(_width, 0)];
        [path addLineToPoint:CGPointMake(_width - _allProgress *(self.progress - _leftProgress), 0)];
    } else if (self.standPoint == RectangleProgressViewStandPointReverse) {
        [path moveToPoint:CGPointMake(_width /2 + _width *0.4 /2 , 0)];
        [path addLineToPoint:CGPointMake(_width, 0)];
        [path addLineToPoint:CGPointMake(_width, _height)];
        [path addLineToPoint:CGPointMake(0, _height)];
        [path addLineToPoint:CGPointMake(0, 0)];
        [path addLineToPoint:CGPointMake(_allProgress *(self.progress - _leftProgress), 0)];
    }
}

@end
